layui.use(['table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 管理
     */
    var DbiShop = {
        tableId: "dbiShopTable"
    };

    /**
     * 初始化表格的列
     */
    DbiShop.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'code', sort: true, title: ''},
            {field: 'name', sort: true, title: ''},
            {field: 'pinyin', sort: true, title: ''},
            {field: 'manageTypeId', sort: true, title: ''},
            {field: 'brandId', sort: true, title: ''},
            {field: 'isEnable', sort: true, title: ''},
            {field: 'regionId', sort: true, title: ''},
            {field: 'provinceId', sort: true, title: ''},
            {field: 'cityId', sort: true, title: ''},
            {field: 'countyId', sort: true, title: ''},
            {field: 'address', sort: true, title: ''},
            {field: 'dogNumber', sort: true, title: ''},
            {field: 'contactTel', sort: true, title: ''},
            {field: 'orderTel', sort: true, title: ''},
            {field: 'fax', sort: true, title: ''},
            {field: 'serverIp', sort: true, title: ''},
            {field: 'isOpen', sort: true, title: ''},
            {field: 'openTime', sort: true, title: ''},
            {field: 'area', sort: true, title: ''},
            {field: 'capacity', sort: true, title: ''},
            {field: 'gcX', sort: true, title: ''},
            {field: 'gcY', sort: true, title: ''},
            {field: 'remark', sort: true, title: ''},
            {field: 'isCenter', sort: true, title: ''},
            {field: 'isLocal', sort: true, title: ''},
            {field: 'delflg', sort: true, title: ''},
            {field: 'createTime', sort: true, title: ''},
            {field: 'creatorId', sort: true, title: ''},
            {field: 'modifyTime', sort: true, title: ''},
            {field: 'modifierId', sort: true, title: ''},
            {field: 'uploadFlg', sort: true, title: ''},
            {field: 'downloadFlg', sort: true, title: ''},
            {field: 'slogan', sort: true, title: ''},
            {field: 'contactPerson', sort: true, title: ''},
            {field: 'centerId', sort: true, title: ''},
            {field: 'createShopId', sort: true, title: ''},
            {field: 'empNum', sort: true, title: ''},
            {field: 'isDataReceive', sort: true, title: ''},
            {field: 'lastTime', sort: true, title: ''},
            {field: 'shopType', sort: true, title: ''},
            {field: 'isSingleCenter', sort: true, title: ''},
            {field: 'isSingleJoint', sort: true, title: ''},
            {field: 'coopCenterId', sort: true, title: ''},
            {field: 'isSingleChange', sort: true, title: ''},
            {field: 'slyTel', sort: true, title: ''},
            {field: 'isEnableMultiTaxRate', sort: true, title: '启用多税率模式, 0 否 1 是'},
            {field: 'invoiceItemId', sort: true, title: '门店开票项, 数据来源arch_invoice_item'},
            {field: 'groupInvoiceInfoNo', sort: true, title: '集团发票信息, 取值范围为1, 2, 3, 4, 5'},
            {field: 'canteenType', sort: true, title: ''},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    DbiShop.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(DbiShop.tableId, {where: queryData});
    };

    /**
     * 弹出添加对话框
     */
    DbiShop.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/dbiShop/add';
    };

    /**
     * 导出excel按钮
     */
    DbiShop.exportExcel = function () {
        var checkRows = table.checkStatus(DbiShop.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    DbiShop.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/dbiShop/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    DbiShop.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dbiShop/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(DbiShop.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DbiShop.tableId,
        url: Feng.ctxPath + '/dbiShop/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: DbiShop.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        DbiShop.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        DbiShop.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        DbiShop.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + DbiShop.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DbiShop.openEditDlg(data);
        } else if (layEvent === 'delete') {
            DbiShop.onDeleteItem(data);
        }
    });
});
